package com.government.area.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.area.vo.ParkAreaTree;
import com.government.area.vo.WxParkAreaTree;
import com.government.common.core.domain.entity.SysUser;
import com.government.common.core.domain.model.LoginUser;
import com.government.common.utils.TreeUtils;
import com.government.system.service.ISysUserService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.transaction.annotation.Transactional;
import com.government.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.government.area.mapper.ParkAreaMapper;
import com.government.area.domain.ParkArea;
import com.government.area.service.IParkAreaService;

import static com.government.common.utils.SecurityUtils.getLoginUser;

/**
 * 园区信息Service业务层处理
 *
 * @author system
 * @date 2023-10-30
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ParkAreaServiceImpl implements IParkAreaService {
    @Autowired
    private ParkAreaMapper parkAreaMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询园区信息
     *
     * @param id 园区信息主键
     * @return 园区信息
     */
    @Override
    public ParkArea selectParkAreaById(String id) {
        return parkAreaMapper.selectParkAreaById(id);
    }

    /**
     * 查询园区信息列表
     *
     * @param parkArea 园区信息
     * @return 园区信息
     */
    @Override
    public List<ParkArea> selectParkAreaList(ParkArea parkArea) {
        return parkAreaMapper.selectParkAreaList(parkArea);
    }

    /**
     * 新增园区信息
     *
     * @param parkArea 园区信息
     * @return 结果
     */
    @Override
    public int insertParkArea(ParkArea parkArea) {
        parkArea.setCreateTime(DateUtils.getNowDate());
        return parkAreaMapper.insertParkArea(parkArea);
    }

    /**
     * 修改园区信息
     *
     * @param parkArea 园区信息
     * @return 结果
     */
    @Override
    public int updateParkArea(ParkArea parkArea) {
        parkArea.setUpdateTime(DateUtils.getNowDate());
        return parkAreaMapper.updateParkArea(parkArea);
    }

    /**
     * 批量删除园区信息
     *
     * @param ids 需要删除的园区信息主键
     * @return 结果
     */
    @Override
    public int deleteParkAreaByIds(String[] ids) {
        return parkAreaMapper.deleteParkAreaByIds(ids);
    }

    /**
     * 删除园区信息信息
     *
     * @param id 园区信息主键
     * @return 结果
     */
    @Override
    public int deleteParkAreaById(String id) {
        return parkAreaMapper.deleteParkAreaById(id);
    }

    /**
     * 查询区园区树结构信息
     *
     * @return
     */
    @Override
    public List<ParkAreaTree> parkAreaTree() {
        List<ParkArea> parkAreaList = parkAreaMapper.selectList(Wrappers.<ParkArea>lambdaQuery());
        Map<Long, List<ParkArea>> areaMap = parkAreaList.stream().collect(Collectors.groupingBy(ParkArea::getParentId));
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = null;
        List<Long> areaList = Lists.newArrayList();


        if (CollectionUtils.isEmpty(parkAreaList)) {

            return null;
        }
        HashMap<Long, List<ParkAreaTree>> allNodeMap = new HashMap<>();

        for (ParkArea item : parkAreaList) {
            Long parentId = Objects.isNull(item.getParentId()) ? 0 : item.getParentId();
            List<ParkAreaTree> nodeList = allNodeMap.get(parentId);
            if (CollectionUtils.isEmpty(nodeList)) {

                nodeList = Lists.newArrayList();
            }
            ParkAreaTree parkAreaTreeVO = new ParkAreaTree();
            parkAreaTreeVO.setId(String.valueOf(item.getId()));
            parkAreaTreeVO.setValue(item.getId());
            parkAreaTreeVO.setLabel(item.getName());
//            if(!loginUser.isVisitor()) {
//                sysUser = loginUser.getUser();
//                if (!"01".equals(sysUser.getUserType())) {
//                    parkAreaTreeVO.setDisabled(false);
//                }
//            }
            nodeList.add(parkAreaTreeVO);
            allNodeMap.put(parentId, nodeList);
        }
        List<ParkAreaTree> rootNode = allNodeMap.get(0L);
        List<ParkAreaTree> parkAreaTreeList = TreeUtils.buildTree(rootNode, allNodeMap);

//        // 游客
//        if (loginUser.isVisitor()) {
//            areaList.add(989L);
//            visitorAreaDataHandler(areaList, parkAreaTreeList);
//        } else { // 注册用户
//            sysUser = sysUserService.getUser();
//            if (!"01".equals(sysUser.getUserType())) {
//               return parkAreaTreeList;
//            }
//            if (sysUser.getVipType() == 2) {
//                registerAreaDataHandler(areaMap, parkAreaTreeList);
//            } else if (sysUser.getVipType() == 3) {
//                for (ParkAreaTree item : parkAreaTreeList) {
//                    for (Object child1 : item.getChildren()) {
//                        vipAreaDataHandler(((ParkAreaTree) child1).getChildren());
//                    }
//                }
//
//            } else if (sysUser.getVipType() == 4) {
//                for (ParkAreaTree item : parkAreaTreeList) {
//                    vipAreaDataHandler(item.getChildren());
//                }
//            }
//        }
        return parkAreaTreeList;
    }

    /**
     * 查询区园区树结构信息
     *
     * @return
     */
    @Override
    public List<WxParkAreaTree> wxParkAreaTree() {
        List<ParkArea> parkAreaList = parkAreaMapper.selectList(Wrappers.<ParkArea>lambdaQuery());
        Map<Long, List<ParkArea>> areaMap = parkAreaList.stream().collect(Collectors.groupingBy(ParkArea::getParentId));
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = null;
        List<Long> areaList = Lists.newArrayList();


        if (CollectionUtils.isEmpty(parkAreaList)) {

            return null;
        }
        HashMap<Long, List<WxParkAreaTree>> allNodeMap = new HashMap<>();

        for (ParkArea item : parkAreaList) {
            Long parentId = Objects.isNull(item.getParentId()) ? 0 : item.getParentId();
            List<WxParkAreaTree> nodeList = allNodeMap.get(parentId);
            if (CollectionUtils.isEmpty(nodeList)) {

                nodeList = Lists.newArrayList();
            }
            WxParkAreaTree parkAreaTreeVO = new WxParkAreaTree();
            parkAreaTreeVO.setId(String.valueOf(item.getId()));
            parkAreaTreeVO.setValue(item.getId());
            parkAreaTreeVO.setLabel(item.getName());
            nodeList.add(parkAreaTreeVO);
            allNodeMap.put(parentId, nodeList);
        }
        List<WxParkAreaTree> rootNode = allNodeMap.get(0L);
        List<WxParkAreaTree> parkAreaTreeList = TreeUtils.buildTree(rootNode, allNodeMap);

        // 游客
//        if (loginUser.isVisitor()) {
//            areaList.add(989L);
//            visitorWxAreaDataHandler(areaList, parkAreaTreeList);
//        } else { // 注册用户
//            sysUser = sysUserService.getUser();
//            if (sysUser.getVipType() == 2) {
//                registerWxAreaDataHandler(areaMap, parkAreaTreeList);
//            } else if (sysUser.getVipType() == 3) {
//                for (WxParkAreaTree item : parkAreaTreeList) {
//                    for (Object child1 : item.getChildren()) {
//                        vipAreaDataHandler(((ParkAreaTree) child1).getChildren());
//                    }
//                }
//
//            } else if (sysUser.getVipType() == 4) {
//                for (WxParkAreaTree item : parkAreaTreeList) {
//                    vipWxAreaDataHandler(item.getChildren());
//                }
//            }
//        }
        return parkAreaTreeList;
    }

    /**
     * 游客地区数据处理
     *
     * @param areaList
     * @param parkAreaTreeList
     */
    private void visitorAreaDataHandler(List<Long> areaList, List<ParkAreaTree> parkAreaTreeList) {
        for (ParkAreaTree item : parkAreaTreeList) {
//            if (areaList.contains(item.getValue())) {
//                item.setDisabled(false);
//            }
            if (CollUtil.isNotEmpty(item.getChildren())) {
                visitorAreaDataHandler(areaList, item.getChildren());
            }
        }
    }

    /**
     * 注册会员地区数据处理
     *
     * @param areaMap
     * @param parkAreaTreeList
     */
    private void registerAreaDataHandler(Map<Long, List<ParkArea>> areaMap, List<ParkAreaTree> parkAreaTreeList) {
        for (ParkAreaTree item : parkAreaTreeList) {

//            if (!areaMap.containsKey(item.getValue())) {
//                item.setDisabled(false);
//            }
            if (CollUtil.isNotEmpty(item.getChildren())) {
                registerAreaDataHandler(areaMap, item.getChildren());
            }
        }
    }

    /**
     * vip地区数据处理
     *
     * @param parkAreaTreeList
     */
    private void vipAreaDataHandler(List<ParkAreaTree> parkAreaTreeList) {

        for (ParkAreaTree item : parkAreaTreeList) {
//            item.setDisabled(false);
            if (Objects.nonNull(item.getChildren())) {
                vipAreaDataHandler(item.getChildren());
            }
        }
    }

    /**
     * 小程序游客地区数据处理
     *
     * @param areaList
     * @param parkAreaTreeList
     */
    private void visitorWxAreaDataHandler(List<Long> areaList, List<WxParkAreaTree> parkAreaTreeList) {
        for (WxParkAreaTree item : parkAreaTreeList) {
//            if (areaList.contains(item.getValue())) {
//                item.setShow(false);
//            }
            if (CollUtil.isNotEmpty(item.getChildren())) {
                visitorWxAreaDataHandler(areaList, item.getChildren());
            }
        }
    }

    /**
     * 小程序注册会员地区数据处理
     *
     * @param areaMap
     * @param parkAreaTreeList
     */
    private void registerWxAreaDataHandler(Map<Long, List<ParkArea>> areaMap, List<WxParkAreaTree> parkAreaTreeList) {
        for (WxParkAreaTree item : parkAreaTreeList) {

//            if (!areaMap.containsKey(item.getValue())) {
//                item.setShow(false);
//            }
            if (CollUtil.isNotEmpty(item.getChildren())) {
                registerWxAreaDataHandler(areaMap, item.getChildren());
            }
        }
    }

    /**
     * 小程序vip地区数据处理
     *
     * @param parkAreaTreeList
     */
    private void vipWxAreaDataHandler(List<WxParkAreaTree> parkAreaTreeList) {

        for (WxParkAreaTree item : parkAreaTreeList) {
//            item.setShow(false);
            if (Objects.nonNull(item.getChildren())) {
                vipWxAreaDataHandler(item.getChildren());
            }
        }
    }
}
